package com.mlink.ai.chat.ui.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.android.billingclient.api.Purchase
import com.cipher.CipherUtils
import com.mlink.ai.chat.network.Api
import com.mlink.ai.chat.network.bean.request.SubscribeRequest
import com.mlink.ai.chat.utils.DeviceUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class AppViewModel(val app: Application) : AndroidViewModel(app) {

    companion object {
        private const val IS_SUBSCRIBED = "is_subscribed"
    }

    private val isSubscribeLiveData = MutableLiveData<Boolean>()

    val purchaseLiveData = MutableLiveData<Pair<String, Purchase>>()


    /**
     * @param product 订阅产品 id
     * @param order 订单 id
     * @param token 购买 token
     */
    fun doSubscribe(product: String, order: String, token: String) {
        viewModelScope.launch(Dispatchers.IO) {
            runCatching {
                val uid = DeviceUtils.getUid()
                val packageName = app.packageName
                val md5Source = order + packageName + product + token + uid + CipherUtils.getCipherKeyFromJNI()
                val md5 = CipherUtils.getMD5(md5Source.toByteArray())
                val request = SubscribeRequest(
                    uid, product, packageName, order, token, md5
                )
                Api.chatApi.doSubscribe(request)
            }.onSuccess {
                if (it.ret == 200) {
                    it.data?.run {
                        setSubscribed(this.isSub)
                    } ?: setSubscribed(true)
                    return@onSuccess
                }
                setSubscribed(true)
            }.onFailure {
                setSubscribed(true)
            }
        }
    }

    fun setSubscribed(isSubscribed: Boolean) {
        if (isSubscribed == isSubscribeLiveData.value) {
            return
        }
        isSubscribeLiveData.postValue(isSubscribed)
        MMKV.defaultMMKV().encode(IS_SUBSCRIBED, isSubscribed)
    }

    fun getSubscribeLiveData() = isSubscribeLiveData

    fun isSubscribed() = MMKV.defaultMMKV().decodeBool(IS_SUBSCRIBED, false)

    /**
     * @param product 订阅产品 id
     * @param order 订单 id
     * @param token 购买 token
     */
    fun doRestore(product: String, order: String, token: String) {
        viewModelScope.launch(Dispatchers.IO) {
            runCatching {
                val uid = DeviceUtils.getUid()
                val packageName = app.packageName
                val md5Source = order + packageName + product + token + uid + CipherUtils.getCipherKeyFromJNI()
                val md5 = CipherUtils.getMD5(md5Source.toByteArray())
                val request = SubscribeRequest(
                    uid, product, packageName, order, token, md5
                )
                Api.chatApi.doRestore(request)
            }.onSuccess {
                if (it.ret == 200) {
                    it.data?.run {
                        setSubscribed(this.isSub)
                    } ?: setSubscribed(true)
                    return@onSuccess
                }
                setSubscribed(true)
            }.onFailure {
                setSubscribed(true)
            }
        }
    }
}
